﻿using AiMoWindows.MainActions;
using AiMoWindows.MainActions.ScriptActions;
using System.Windows.Input;
namespace AiMoWindows.Windows.Scripts.ViewModels
{
    class ScriptViewModel : ViewModelBase
    {
        public ScriptUIViewModel ScriptUI { get; }
        public ScriptContextMenuViewModel ScriptWindowMenu { get; }
        readonly ActionsController actions;
        public ScriptViewModel(ScriptWindow mainWindow, ActionsController actions, RenameSymbolDialog renameDialog)
        {
            this.actions = actions;
            ScriptUI = new ScriptUIViewModel(mainWindow);
            ScriptWindowMenu = new ScriptContextMenuViewModel(actions);
            ScriptUI.AppendLanguageChangeModel(ScriptWindowMenu);
            RunCommand = new RelayCommand(actions.MainAction.RunButton_Click);
            AiAssistantCommand = new RelayCommand(actions.MainAction.OpenAiWindow);
            LibraryCommand = new RelayCommand(actions.MainAction.OpenCommandLibraryWindow);
            OpenCurrentScriptPathCommmand = new RelayCommand(OpenCurrentScriptPath_Click);
            FormatScriptCommand = new RelayCommand(() =>
            {
                actions.ClearDebugPrint();
                actions.SyncAction.SyncScriptsWithFormat().ConfigureAwait(false);

            });
            CommmentScriptCommand = new RelayCommand(() => actions.CommentAction.CommentUncommentSelectionAsync(CommentActionType.Comment).ConfigureAwait(false));
            UncommentScriptCommand = new RelayCommand(() => actions.CommentAction.CommentUncommentSelectionAsync(CommentActionType.Uncomment).ConfigureAwait(false));
            RenameSymbolCommand = new RelayCommand(() => actions.NameMatchAction.RenameSymbolAsync(renameDialog).ConfigureAwait(false));
            GoToLineCommand = new RelayCommand(actions.DefinitionAction.GotoDefinition);
        }
        public ICommand RunCommand { get; }
        public ICommand AiAssistantCommand { get; }
        public ICommand LibraryCommand { get; }
        public ICommand FormatScriptCommand { get; }
        public ICommand CommmentScriptCommand { get; }
        public ICommand UncommentScriptCommand { get; }
        public ICommand GoToLineCommand { get; }
        public ICommand RenameSymbolCommand { get; }
        public ICommand OpenCurrentScriptPathCommmand { get; }
        bool _IsReadOnly = false;
        public bool IsReadOnly
        {
            get => _IsReadOnly;
            set
            {
                _IsReadOnly = value;
                RaisePropertyChanged(nameof(IsReadOnly));
            }
        }
        string _CurrentScriptPath = "";
        public string CurrentScriptPath
        {
            get => _CurrentScriptPath;
            set
            {
                _CurrentScriptPath = value;
                RaisePropertyChanged(nameof(CurrentScriptPath));
                ScriptUI.TitleSuffix = System.IO.Path.GetFileNameWithoutExtension(_CurrentScriptPath);
            }
        }
        string _CursorLocationInfo = "";
        public string CursorLocationInfo
        {
            get => _CursorLocationInfo;
            set
            {
                _CursorLocationInfo = value;
                RaisePropertyChanged(nameof(CursorLocationInfo));
            }
        }
        public void Update_Finished(object? sender, EventArgs e)
        {
            actions.FormatAction.ReformatAfterChangeAsync();
        }
        public void Document_Changed(object? sender, ICSharpCode.AvalonEdit.Document.DocumentChangeEventArgs e)
        {
            // 当没有初始化，且没有在Format中，没有在BringBack时，才检测Change
            actions.FormatAction.IfNeedReFormatWhenChanged(e);
        }
        public void OnSavePathChanged(string path)
        {
            CurrentScriptPath = path;
        }
        private void OpenCurrentScriptPath_Click()
        {
            if (!string.IsNullOrEmpty(CurrentScriptPath))
                ActionsUtils.SelectFileInWindows(CurrentScriptPath);
        }
    }
}